home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / kriegspi / check.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-30  |  1.8 KB  |  78 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: check.c,v 1.4 87/02/12 13:21:04 schoch Exp $";
  3. #endif
  4.  
  5. /* check.c */
  6. #include "externs.h"
  7.  
  8. moveintocheck (from, to)
  9.     int from, to;
  10. {
  11.     int victim, intocheck;
  12.     u_char color;
  13.     LIST check ();
  14.  
  15.     color = whose [from];            /* make move on board */
  16.     victim = findvictim (from, to);
  17.     if (victim)
  18.         whose [victim] = EMPTY;
  19.     whose [to] = color;
  20.     whose [from] = EMPTY;
  21.     if (occupant [from] == KING)
  22.         kingloc [color] = to;
  23.     intocheck = (check (color) != NIL);    /* see if now in check */
  24.     if (occupant [from] == KING)        /* restore board position */
  25.         kingloc [color] = from;
  26.     whose [from] = color;
  27.     whose [to] = EMPTY;
  28.     if (victim)
  29.         whose [victim] = 1 - color;
  30.     return intocheck;
  31. }
  32.  
  33. LIST
  34. check (color)
  35. u_char color;
  36. {
  37.     LIST l, checkdirs, lmember (), linsert ();
  38.     int direction, spot, dist, side;
  39.  
  40.     checkdirs = NIL;
  41.     l = dirlist [QUEEN];
  42.     while (l != NIL) {
  43.         direction = l->i;
  44.         l = l->n;
  45.         spot = kingloc [color];
  46.         for (dist = 1; TRUE; dist++) {
  47.             spot += direction;
  48.             if (spot < 0 || spot > 99)
  49.                 continue;
  50.             if ((whose [spot] == 1 - color)
  51.             && (lmember (-direction, dirlist [occupant [spot]])
  52.                 && !(occupant [spot] == KING && dist > 1)))
  53.                 checkdirs = linsert (checkdirs, direction);
  54.             if (whose [spot] != EMPTY)
  55.                 break;
  56.         }
  57.     }
  58.     l = dirlist [KNIGHT];
  59.     while (l != NIL) {
  60.         direction = l->i;
  61.         l = l->n;
  62.         spot = kingloc [color] + direction;
  63.         if (spot < 0 || spot > 99)
  64.             continue;
  65.         if (whose [spot] == 1 - color && occupant [spot] == KNIGHT)
  66.             checkdirs = linsert (checkdirs, direction);
  67.     }
  68.     for (side = -1; side <= 1; side += 2) {
  69.         spot = kingloc [color] + pawndir [color] + side;
  70.         if (spot < 0 || spot > 99)
  71.             continue;
  72.         if (whose [spot] == 1 - color && occupant [spot] == PAWN)
  73.             checkdirs = linsert(checkdirs, pawndir [color] + side);
  74.     }
  75.  
  76.     return checkdirs;
  77. }
  78.